package com.madi.company.ui.usercenter;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.baserx.RxSubscriber;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.madi.company.MainActivity;
import com.madi.company.R;
import com.madi.company.api.Api;
import com.madi.company.api.ApiService;
import com.madi.company.app.GlobalApplication;
import com.madi.company.bean.UserInfoEntity;
import com.madi.company.utils.CheckCode;
import com.madi.company.utils.encryption.RSACoder;
import com.madi.company.widget.InputMethodRelativeLayout;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity {

    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.textOne)
    TextView mTextOne;
    @Bind(R.id.textView8)
    TextView mTextView8;
    @Bind(R.id.loginPicture)
    RelativeLayout mLoginPicture;
    @Bind(R.id.country)
    TextView mCountry;
    @Bind(R.id.loginCountryName)
    TextView mLoginCountryName;
    @Bind(R.id.loginCountryCode)
    Spinner mLoginCountryCode;
    @Bind(R.id.codeValue)
    TextView mCodeValue;
    @Bind(R.id.view)
    View mView;
    @Bind(R.id.userName)
    EditText mUserName;
    @Bind(R.id.usercenterPassword)
    TextView mUsercenterPassword;
    @Bind(R.id.pass)
    EditText mPass;
    @Bind(R.id.VerifyPassword)
    TextView mVerifyPassword;
    @Bind(R.id.password_once)
    EditText mPasswordOnce;
    @Bind(R.id.okBtn)
    Button mLoginBtn;
    @Bind(R.id.forgetBtn)
    Button mForgetBtn;
    @Bind(R.id.scroll)
    ScrollView mScroll;
    @Bind(R.id.loginlayout)
    InputMethodRelativeLayout mLoginlayout;
    @Bind(R.id.iv_img_backgroud)
    ImageView mImgBackground;
    @Override
    public int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        mTextOne.setText(R.string.register);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation animation = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.translate_anim);
                mImgBackground.startAnimation(animation);
            }
        }, 200);
    }

    @OnClick({R.id.okBtn, R.id.forgetBtn, R.id.textOne})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.okBtn://登錄
                login();
                break;
            case R.id.forgetBtn: // 忘記密碼
                break;
            case R.id.textOne:// 註冊

                break;
        }
    }

    private void login() {
        if (CheckCode.isNumeric(mUserName.getText().toString()) && mUserName.getText().toString().trim().length() > 0) {
            if (mPass.getText().toString().trim().length() > 0) {
                try {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("phone", mUserName.getText().toString());
                    map.put("password", RSACoder.encrypt(mPass.getText().toString()));
                    map.put("clientType", "Android");
                    map.put("areaCode", "");
                    Api.getInstance().get(ApiService.class).getUserInfo(Api.getCacheControl(), map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                            subscribe(new RxSubscriber<UserInfoEntity>(this, true) {
                                @Override
                                protected void _onNext(UserInfoEntity userInfoEntity) {
                                    if (userInfoEntity != null) {
                                        GlobalApplication.setAccessToken(userInfoEntity.getAccess_token());
                                        GlobalApplication.setRefreshToken(userInfoEntity.getRefresh_token());
                                        GlobalApplication.getInstance().setUserModel(userInfoEntity.getUser());

                                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                        finish();
                                    }
                                }

                                @Override
                                protected void _onError(String message) {
                                    ToastUitl.showLong(message);

                                }
                            });
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUitl.showLong(R.string.login_fail);
                }
            } else {
                ToastUitl.showLong(R.string.write_pass);
            }
        } else {
            ToastUitl.showLong(R.string.write_phone);
        }
    }
}
